/*
 * RBFCommissioning.h
 *
 *  Created on: 2023年7月12日
 *      Author: fjx
 */

#ifndef MAC_RBFCOMMISSIONING_H_
#define MAC_RBFCOMMISSIONING_H_
#include "RBFCommon.h"
#include "device.h"
#define __packed  __attribute__((__packed__))

typedef enum{
  /*!
   * 节点寻呼数据解析状态
   */
  RBF_COMMSISSIONING_NODE_PAGING_HDR_PARSE_TRUE=0,
  RBF_COMMSISSIONING_NODE_PAGING_HDR_PARSE_FALSE,
  RBF_COMMSISSIONING_NODE_PAGING_PYD_PARSE_TRUE,
  RBF_COMMSISSIONING_NODE_PAGING_PYD_PARSE_FALSE,
  RBF_COMMSISSIONING_NODE_PAGING_MSG_LEN_ERROR,
  RBF_COMMSISSIONING_NODE_PAGING_CMD_ERROR,



  /*!
   * 节点入网确认数据解析状态
   */
  RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_HDR_PARSE_TRUE,
  RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_HDR_PARSE_FALSE,
  RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_PYD_PARSE_TRUE,
  RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_PYD_PARSE_FALSE,
  RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_MSG_LEN_ERROR,
  RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_CMD_ERROR,


  /*!
   * 网关允许节点入网数据解析状态
   */
  RBF_COMMSISSIONING_GW_ALLOW_JOIN_HRD_PARSE_TRUE,
  RBF_COMMSISSIONING_GW_ALLOW_JOIN_HRD_PARSE_FALSE,
  RBF_COMMSISSIONING_GW_ALLOW_JOIN_PYD_PARSE_TRUE,
  RBF_COMMSISSIONING_GW_ALLOW_JOIN_PYD_PARSE_FALSE,
  RBF_COMMSISSIONING_GW_ALLOW_JOIN_MSG_LEN_ERROR,
  RBF_COMMSISSIONING_GW_ALLOW_JOIN_CMD_ERROR,



}RBFCommissioningStatus_t;

/*
 * cmd = 0x01  节点寻呼
 * payload: DeviceType(1B) + ModelType(1B) + HardType(1B) + SN(4B) + RFVer(3B) + sVer(3B) + KeyCodeA(16B) + SN(16B)
 *
 */
typedef struct {
//  uint8_t   *buffer;
//  uint8_t   buffer_size;



  uint16_t  macAdd2;        //[15]
  uint8_t   msgSeq;         //[17]
  uint8_t   msgRetry;       //[18]
  uint8_t   battV;          //[19]
  uint8_t   rssi;           //[20]
  uint8_t   temp;           //[21]
  uint8_t   qrfR1;          //[22]
  uint8_t   qrfR2;          //[23]
  uint8_t   echASeq;        //[24]

  uint8_t   deviceType;     //[25]
  uint8_t   modelType;      //[26]
  uint8_t   hardType;       //[27]
  uint64_t  macAdd8;        //[28~31,32~35]  8Bytes
  uint32_t  rfVer:24;       //[36~38] 3Bytes
  uint32_t  sVer:24;        //[39] app版本改为3Byte
  uint8_t   keyCodeA[RBF_KEY_LENGTH_MAX];//[40] 16Bytes
  uint8_t   sn[SN_LENGTH];  //16Bytes
  uint32_t  mac;            //4Bytes
}__packed RBFMacMessageJoinRequest_t;

typedef enum{
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_FIRST = RBF_MAC_HEADER_INDEX_LAST+1,//15,
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2  = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_FIRST, //MAC add //15
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MSGSEQ = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2+2,   //17
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MSGRETRY = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2+3,
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_BATTV = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2+4,
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_RSSI = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2+5,
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_TEMP = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2+6,
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_QRFR1 = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2+7,
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_QRF2 = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2+8,
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_ECHASEQ = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2+9,
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_DEVICETYPE = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2+10,
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MODELTYPE = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2+11,
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_HARDTYPE = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2+12,
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD8 = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2+13, //[28~31,32~35]
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_RFVER = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2+21,//[36]
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_SVER = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2+24,//sver由原来的1Byte改为3Byte
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_KEYCODEA = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2+25+2,
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_SN = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2+41+2,
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MAC = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2+41+16+2,
  RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_LAST = RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MAC,
}RBFMAC_PAYLOAD_JOIN_REQ_INDEX_t;

#define RBF_MAC_PAYLOAD_JOIN_REQ_LEN  sizeof(RBFMacMessageJoinRequest_t) //(RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_LAST-RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_FIRST+4)//

#if defined (PACKET_0_INCLUDE)
#define RBF_MAC_PACKET_JOIN_REQ_LEN     (RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_JOIN_REQ_LEN)
#else
#define RBF_MAC_PACKET_JOIN_REQ_LEN     (RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_JOIN_REQ_LEN)
#endif

/*
 * CMD = 0x02  网关加入网络
 * payload: SN(4B) + RollingCode(4B) + SystemID(3B) + LocalID(2B) + KeyCodeB(16B) + SystemKeyCode(16B) + broadcastKeyCode(16B)
 *
 * SystemKeyCode ase(A+B)
 *
 * GATEWAY_ALLOW_JOIN_NETWORK
 */
typedef struct{
  uint32_t  macAdd4;                   //4Bytes,对应RFM LOW Macc 4Bytes,子设备用来校验[0]
                                      //用于多个RFM设备同时入网时,RFM作为是否是HUB回应自己校验使用

  uint32_t  rollingCode;              //4Bytes[4]
  uint32_t  systemID:24;              //3Bytes[8]
  uint16_t  localID;                  //2Bytes[11]
  uint8_t   keyCodeB[RBF_KEY_LENGTH_MAX];          //16Bytes[13]
  uint8_t   systemKeyCode[RBF_KEY_LENGTH_MAX];     //用于漫游消息认证 16Bytes[29]
  uint8_t   broadcastKeyCode[RBF_KEY_LENGTH_MAX];  //用于广播消息认证 16Bytes[45]
  uint32_t  mac;                      //[61]
}__packed RBFMacMessageGWAlloawJoinNetwork_t;

typedef enum{
  RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_FIRST = RBF_MAC_HEADER_INDEX_LAST+1,//15,
  RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_MACADD4  = RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_FIRST, //MAC add //15
  RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_ROLLINGCODE = RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_FIRST+4,
  RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_SYSTEMID = RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_FIRST+8,
  RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_LOCALID = RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_FIRST+11,
  RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_KEYCODEB = RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_FIRST+13,
  RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_SYSTEMKEYCODE = RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_FIRST+29,
  RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_BROADCASTKEYCODE = RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_FIRST+45,

  RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_MAC = RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_FIRST+61,
  RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_LAST = RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_MAC,
}RBFMAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_t;

#define RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NETWORK_LEN   sizeof(RBFMacMessageGWAlloawJoinNetwork_t)

#if defined (PACKET_0_INCLUDE)
#define RBF_MAC_PACKET_GW_ALLOW_JOIN_NETWORK_LEN      (RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NETWORK_LEN)
#else
#define RBF_MAC_PACKET_GW_ALLOW_JOIN_NETWORK_LEN      (RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NETWORK_LEN)
#endif


/*
 * CMD = 0x03  节点入网确认
 * payload: MsgSeq(1B) + MsgRetry(1B) + BattV(1B) + RSSI(1B) + Temp(1B)
 *          + MacAdd(2B) + QrfR1(1B) + QrfR2(1B) + EchASeq(1B) + DeviceType(1B)
 *          + ModelType(1B) + HardType(1B) + SN(4B) + RFVer(3B) + sVer(3B)
 *
 *
 */
typedef struct{

  uint8_t   msgSeq;         //[15]
  uint8_t   msgRetry;       //[16]
  uint8_t   battV;          //[17]
  uint8_t   rssi;           //[18]
  uint8_t   temp;           //[19]
  uint16_t  macAdd2;        //[20,21] 2Bytes
  uint8_t   qrfR1;          //[22]
  uint8_t   qrfR2;          //[23]
  uint8_t   echASeq;        //[24]

  uint8_t   deviceType;     //[25]
  uint8_t   modelType;      //[26]
  uint8_t   hardType;       //[27]
  uint64_t  macAdd8;             //[28~31,32~35]  8Bytes
  uint32_t  rfVer:24;       //[36~38] 3Bytes
  uint32_t  sVer:24;        //[39]sver改为3Byte
  uint8_t   sn[SN_LENGTH];

  uint32_t  mac;            //4Bytes

}__packed RBFMacMessageNodeJoinConfirm_t;

#define JOIN_CONFIRM_WITH_KEYVALUE
typedef enum{
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST = RBF_MAC_HEADER_INDEX_LAST+1,//15,
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MACADD2 = RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST, //2bytes//15
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MEGSEQ  = RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST+2, //17
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MSGRETRY = RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST+3, //18
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_BATTV = RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST+4,  //19
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_RSSI = RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST+5,  //20
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_TEMP = RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST+6,  //21
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_QRFR1 = RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST+7, //22
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_QFRR2 = RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST+8, //23
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_ECHASEQ = RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST+9,//24
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_DEVICETYPE = RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST+10,//25
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MODELTYPE = RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST+11, //26
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_HARDTYPE = RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST+12, //27
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MACADD8 = RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST+13, //28
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_RFVER = RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST+21, //36
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_SVER = RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST+24,
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_SN  = RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST+25+2, //sver由原来额1Byte改为3Byte
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MAC = RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST+25+16+2,
  RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_LAST = RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MAC,
}RBFMAC_PAYLOAD_JOIN_CONFIRM_INDEX_t;
#define RBF_MAC_PAYLOAD_JOIN_CONFIRM_LEN  sizeof(RBFMacMessageNodeJoinConfirm_t)//(RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_LAST-RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST+4)//

#if defined (PACKET_0_INCLUDE)
#define RBF_MAC_PACKET_JOIN_CONFIRM_LEN    (RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_JOIN_CONFIRM_LEN)
#else
#define RBF_MAC_PACKET_JOIN_CONFIRM_LEN    (RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_JOIN_CONFIRM_LEN)
#endif

void RBF_set_node_paging(RBFMacMessageJoinRequest_t msg,uint8_t *packet);
RBF_RET_STATUS RBF_get_node_paging_packet_parse(uint8_t *packet,uint16_t packet_len,RBFMacMessageJoinRequest_t *msg);//uint8_t *codeA,uint8_t *macAdd8);


void RBF_set_node_join_confirm(RBFMacMessageNodeJoinConfirm_t msg,uint8_t *packet);
RBF_RET_STATUS RBF_get_node_join_confirm(uint8_t *packet,uint16_t packet_len,RBFMacMessageNodeJoinConfirm_t *msg);

void RBF_set_gw_allow_join_net(RBFMacHeader_t hdr,RBFMacMessageGWAlloawJoinNetwork_t msg,uint8_t * packet);
/*
 *@note
 *
 *
 */
RBF_RET_STATUS RBF_get_gw_allow_join_net(uint8_t *packet,uint16_t packet_len);

void RBF_gw_allow_join_net_payload_packetToMsg(uint8_t *packet,RBFMacMessageGWAlloawJoinNetwork_t *msg);
void RBF_node_join_confirm_payload_packetToMsg(uint8_t *packet,RBFMacMessageNodeJoinConfirm_t *msg);


#endif /* MAC_RBFCOMMISSIONING_H_ */
